/*
 * table_TempLinkage.c
 *
 *  Created on: 2012/03/29
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"

#define TableSize  (sizeof(TEMPLINKAGE))
#define TableType  PTEMPLINKAGE
#define stTable stTempLinkageManager.memoryBlocks
#define maxTable stTempLinkageManager.maxRecordNum
#define  maxNumPerBlk  (stTempLinkageManager.astMaxNum_Block)
/*KIND_GBLLINKAGE*/
typedef struct _GBLLINKAGE
{
	AST		astSymDefInfo;
	AST     astTen;
	AST     astDec;
	BOOL	bRef;
	BOOL    bExternal;
	BOOL    bFunction;
	RBTREE rbtree;
}TEMPLINKAGE, *PTEMPLINKAGE;
static AST      gblLinkage=0;
/* get the object */
static TABLEMANAGER stTempLinkageManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stTempLinkageManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stTempLinkageManager,  resetListener, index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	gblLinkage = 0;
	return Clear_Com( &stTempLinkageManager );
}
static BOOL ResetTable( VOID )
{
	gblLinkage = 0;
	return Reset_Com( &stTempLinkageManager );
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stTempLinkageManager, astReqArg, resetListener );
}
static AST getMaxLineNum( VOID ){
	return maxTable;
}
/* give up the way that defining get/set function pointer in the struct,
 * because of memory volume.
 */
//	IDX_DECSYMCHAIN
/*  get method */
static AST  getDefinition( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astSymDefInfo;
}
/* set method */
static AST  setDefinition( AST index, AST idx )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->astSymDefInfo = idx;
}
static AST  getTen( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astTen;
}
/* set method */
static AST  setTen( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->astTen = value;
}
static AST  getDec( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astDec;
}
/* set method */
static AST  setDec( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->astDec = value;
}
/*  get method */
static BOOL  getReferInfo( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->bRef;
}
/* set method */
static BOOL  setReferInfo( AST index, BOOL value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->bRef = value;
}
/*  get method */
static BOOL  getExternalFlag( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->bExternal;
}
/* set method */
static BOOL  setExternalFlag( AST index, BOOL value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->bExternal = value;
}
/*  get method */
static BOOL  getFunctionFlg( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->bFunction;
}
/* set method */
static BOOL  setFunctionFlg( AST index, BOOL value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->bFunction = value;
}
static AST getLeft( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild;
}
static AST setLeft( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild = value;
}
static AST getRight( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild;
}
static AST setRight( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild = value;
}
static AST getParent( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent;
}
static AST setParent( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent = value;
}
static AST getIndex( AST index )
{
	return index;
}
static U2 getColor( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor;
}
static U2 setColor( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor = value;
}
static ACCESS_RBTREE stAccessRbtree =
{
		AllocTable,
		getMaxLineNum,
		getIndex,
		{
				getLeft,
				setLeft
		},
		{
				getRight,
				setRight
		},
		{
				getParent,
				setParent
		},
		{
				getColor,
				setColor
		}
};
static S4  cmpSymbolName( AST astIndex, void * pstrName )
{
	PSZ  pszDefinedName;
	PSZ  pszNewName;
	AST  astSymbol;
	S4 ret;

	pszNewName = ( PSZ )pstrName;
	if( pszNewName == NULL )
	{
		return RBT_LESS;
	}
	astSymbol = stTempLinkageAccess.decVari.get( astIndex );
	if( stTempLinkageAccess.bFunction.get( astIndex ) ){
		pszDefinedName = stFunctionStorageAccess.name.get( astSymbol );
	}else{
		pszDefinedName = stVariableStorageAccess.name.get( astSymbol );
	}
	ret = strcmp(pszDefinedName, pszNewName );
	if( ret == 0 )
	{
		return RBT_FOUND;
	}
	if( ret < 0 )
	{
		return RBT_LESS;
	}
	return RBT_LARGER;
}
static AST  findLinkage( PSZ  name ){
	AST astResult;
	S4 ret;
	ret = FindData( &stAccessRbtree, gblLinkage, name,
			&astResult, cmpSymbolName );
	if( ret == RBT_FOUND )
	{
		return astResult;
	}
	return 0;
}
static AST  addLinkage( PSZ name ){
	AST astResult;
	S4 find_result;
	AST nMaxIndex=0;
	BOOL bret;

	find_result = FindData( &stAccessRbtree, gblLinkage, name, &astResult, cmpSymbolName );
	if( find_result == RBT_FOUND )
	{
		return astResult;
	}
	bret =  InsertData( &stAccessRbtree, find_result, astResult, &gblLinkage, &nMaxIndex );
	if( bret == FALSE  )
	{
		return (0);
	}
	return nMaxIndex;
}
const TEMPLINKAGE_ACCESS stTempLinkageAccess = {

				ClearTable,
				ResetTable,
				AllocTable,
				getMaxLineNum,

		{
				getDefinition,
				setDefinition
		},
		{
				getTen,
				setTen
		},
		{
				getDec,
				setDec
		},
		{
				getReferInfo,
				setReferInfo
		},

		{
				getExternalFlag,
				setExternalFlag
		},
		{
				getFunctionFlg,
				setFunctionFlg
		},
		findLinkage,
		addLinkage
};
/* end of file */

